package com.work.rcj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.work.rcj.common.base.BaseController;
import com.work.rcj.common.model.Response;
import com.work.rcj.common.model.page.PageResult;
import com.work.rcj.entity.Medical;
import com.work.rcj.entity.Worker;
import com.work.rcj.pojo.dto.MedicalDto;
import com.work.rcj.pojo.dto.WorkerDto;
import com.work.rcj.service.MedicalService;
import com.work.rcj.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * 医疗设备管理
 *
 * @author rcj
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/medical")
public class MedicalController extends BaseController {
    @Autowired
    private MedicalService medicalService;


    /**
     * 医疗设备列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public Response<PageResult<Medical>> VisitList(MedicalDto dto) {
        Page<Medical> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        QueryWrapper<Medical> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("deleted",1);
        if(!StringUtils.isEmpty(dto.getSearchKey())){
            queryWrapper.like("name",dto.getSearchKey());
        }
        IPage<Medical> list = medicalService.page(page, queryWrapper);
        return Response.success(getDataTable(list));
    }

    /**
     * 新增医疗设备
     *
     * @param req
     * @return
     */
    @PostMapping("/save")
    public Response save(@RequestBody Medical req) {
        medicalService.save(req);
        return Response.success();
    }

    /**
     * 修改医疗设备信息
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public Response update(@RequestBody Medical req) {
        UpdateWrapper<Medical> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", req.getId());
        medicalService.update(req, updateWrapper);
        return Response.success();
    }

    /**
     * 删除医疗设备
     *
     * @param req
     * @return
     */
    @PostMapping("/del")
    public Response del(@RequestBody Medical req) {
        Medical entity = new Medical();
        entity.setId(req.getId());
        entity.setDeleted("1");
        medicalService.updateById(entity);
        return Response.success();
    }

    /**
     * 医疗设备详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Response<Medical> detail(Long id) {
        Medical medical = medicalService.getById(id);
        return Response.success(medical);
    }
}

